
local runtime = require 'jass.runtime'
local ffi = require 'ffi'
ffi.cdef [[
    // 修改窗口标题
    int SetWindowTextA(int hwnd, const char* lpString);
    // 修改程序img
    typedef void* HANDLE;
    typedef HANDLE HICON;
    typedef long LONG_PTR;
    typedef long DWORD;
    HICON LoadImageA(HANDLE hinst, const char* lpszName, unsigned int uType, int cxDesired, int cyDesired, unsigned int fuLoad);
    int SendMessageA(int hwnd, unsigned int Msg, unsigned int wParam, LONG_PTR lParam);
    DWORD GetLastError();
    bool SetWindowTextW(int hwnd, const wchar_t* lpString);
    int MultiByteToWideChar(unsigned int CodePage, unsigned long dwFlags, const char* lpMultiByteStr, int cbMultiByte, wchar_t* lpWideCharStr, int cchWideChar);
    int WideCharToMultiByte(unsigned int CodePage, unsigned long dwFlags, const wchar_t* lpWideCharStr, int cchWideChar, char* lpMultiByteStr, int cchMultiByte, const char* lpDefaultChar, int* pfUsedDefaultChar);
]]

local war3_hwnd = 0
local _, game_base = xpcall(ffi.C["GetModuleHandleA"], runtime["error_handle"], "Game.dll")
local _, war3_hwnd = xpcall(ffi["cast"], runtime["error_handle"], "int *", game_base + 12429772)
if war3_hwnd then
    war3_hwnd = war3_hwnd[0]
else
    war3_hwnd = 0
end

-- utf8编码转换
local function utf8_to_wide_string(utf8_str)
    local CP_UTF8 = 65001
    local size_needed = ffi.C["MultiByteToWideChar"](CP_UTF8, 0, utf8_str, #utf8_str, nil, 0)
    local wide_str = ffi.new("wchar_t[?]", size_needed + 1)
    ffi.C["MultiByteToWideChar"](CP_UTF8, 0, utf8_str, #utf8_str, wide_str, size_needed)
    return wide_str
end

local mt = {}
-- 设置窗口标题
function mt.set_window_title(title)
    local wide_title = utf8_to_wide_string(title)
    print(title,war3_hwnd, wide_title)
    xpcall(ffi.C["SetWindowTextW"], runtime["error_handle"], war3_hwnd, wide_title)
end

-- 设置窗口图标
function mt.set_window_icon(path)
    local icon_path = ("%s\\resources\\map_img.ico"):format("lightning")
    storm.save(icon_path, storm.load(path))
    local WM_SETICON = 0x0080
    local ICON_SMALL = 0
    local ICON_BIG = 1
    local IMAGE_ICON = 1
    local LR_LOADFROMFILE = 0x0010
    local hIcon = ffi.C['LoadImageA'](nil, icon_path, IMAGE_ICON, 0, 0, LR_LOADFROMFILE)
    if hIcon == nil then
        local error_code = ffi.C["GetLastError"]()
        print("加载图标失败:" .. tostring(error_code))
        return
    end
    local result_small = ffi.C["SendMessageA"](war3_hwnd, WM_SETICON, ICON_SMALL, ffi.cast("LONG_PTR", hIcon))
    if result_small == 0 then
        local error_code = ffi.C["GetLastError"]()
        print("加载图标失败:" .. tostring(error_code))
        print("设置小图标失败: " .. icon_path .. ", error: " .. tostring(error_code))
    else
        print("小图标设置成功: " .. icon_path)
    end
    local result_big = ffi.C["SendMessageA"](war3_hwnd, WM_SETICON, ICON_BIG, ffi.cast("LONG_PTR", hIcon))
    if result_big == 0 then
        local error_code = ffi.C["GetLastError"]()
        print("设置大图标失败: " .. icon_path .. ", error: " .. tostring(error_code))
    else
        print("大图标设置成功: " .. icon_path)
    end
    print("图标设置完成: " .. icon_path)
end

-- 修改魔兽窗口标题和图标
-- local window_title = string.format("%s",japi['GetMapName']())
-- mt.set_window_title(window_title)
-- mt.set_window_icon('ReplaceableTextures\\CommandButtons\\BTNReturnGoods.blp')